﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test_ProDivingJudge2salmon14
{
    public partial class FormMain : Form
    {
        public BackgroundWorker receiveWorker;
        public FormMain()
        {
            InitializeComponent();

            OkButton.Text = "OK";
            OkButton.Click += new EventHandler(OnOK);

            SendButton.Enabled = false;
            SendButton.Text = "Send score";
            SendButton.Click += new EventHandler(OnSend);

            ScoreBox.Enabled = false;

            IDBox.Text = "Enter Judge ID here";

            ScoreBox.Text = "Enter score";

            receiveWorker = new BackgroundWorker();
            receiveWorker.WorkerSupportsCancellation = true;
            receiveWorker.DoWork += new DoWorkEventHandler(GetData);
            receiveWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(GetDataDone);

        }

        private void GetDataDone(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true) 
            {
                System.Console.WriteLine("ReceiveWorker canceled!");
            }
            else if (!(e.Error == null))
            {
                System.Console.WriteLine("An error with ReceiveWorker occured!");
            }
            else 
            {
                System.Console.WriteLine("ReceiveWorker DONE!");
                DiveBox.Text = DataManager.received;
                ScoreBox.Text = "";
                ScoreBox.Enabled = true;
            }
        }

        private void GetData(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = (BackgroundWorker)sender;
            
            DataManager.received = "";
            while (DataManager.received == "") 
            {
                if (worker.CancellationPending == true)
                {
                    e.Cancel = true;
                    break;
                }
                else 
                {
                    DataManager.ReceiveData();
                }
            }
        }

        public void OnOK(object sender, EventArgs e) 
        {
            DataManager.LoopConnect();
            DataManager.judgeID = Int32.Parse(IDBox.Text);
            DataManager.send = ":" + IDBox.Text;
            DataManager.SendData();
            DataManager.ReceiveData();

            JudgeBox.Text = "Judge: " + DataManager.received;
            
            OkButton.Enabled = false;
            IDBox.Enabled = false;

            SendButton.Enabled = true;

            this.NextDive();
        }

        public void OnSend(object sender, EventArgs e) 
        {
            DataManager.send = DataManager.judgeID + "," + ScoreBox.Text;
            DataManager.SendData();
            DataManager.ReceiveData();

            if (DataManager.received == "RECORDED")
            {
                ScoreBox.Text = "Score SENT!";
                DiveBox.Text = "Waiting for a dive";
                ScoreBox.Enabled = false;
            }
            else
            {
                ScoreBox.Text = "ERROR!";
            }

            this.NextDive();
        }

        private void NextDive() 
        {
            DiveBox.Text = "Waiting for a dive";
            if (!receiveWorker.IsBusy)
            {
                receiveWorker.RunWorkerAsync();
            }
        }
    }
}
